package com.jrapider.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.kaptcha.Kaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jrapider.manager.data.om.SysMenu;
import com.jrapider.manager.data.om.SysUser;
import com.jrapider.manager.service.menu.SysMenuService;
import com.jrapider.manager.service.user.SysUserService;
import com.jrapider.om.JSONResult;
import com.jrapider.session.UserSession;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2018/11/5.
 */
@Controller
public class IndexController {

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    private Kaptcha kaptcha;

    @Value("${jrapider.login-page-user:/page/login}")
    private String loginPageUrl;

    @RequestMapping("")
    @ApiOperation(value = "", httpMethod = "GET")
    public String index(Model model) {
        QueryWrapper<SysMenu> qw = new QueryWrapper<SysMenu>().orderByAsc("sortcode");
        List<SysMenu> menus = sysMenuService.list(qw);
        model.addAttribute("menus", JSONObject.toJSONString(menus));
        SysUser sysUser = UserSession.getUser();
        model.addAttribute("userName",sysUser.getName());
        return "index";
    }

    @RequestMapping("/page/login")
    @ApiOperation(value = "登录页", httpMethod = "GET")
    public String loginView() {
        return "login";
    }

    @RequestMapping("/page/changepwd")
    @ApiOperation(value = "修改密码页", httpMethod = "GET")
    public String changePwdView() {
        return "changepwd";
    }

    @ApiOperation("登录")
    @RequestMapping(
            value = {"/data/login"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JSONObject login(String loginId, String password,String code) {
        kaptcha.validate(code);
        SysUser sysUser = sysUserService.validate(loginId, password);
        if (sysUser == null) {
            return JSONResult.newBuilder().fail("用户名或者密码错误").build();
        } else {
            UserSession.setUser(sysUser);
            return JSONResult.newBuilder().success("登录成功").build();
        }
    }

    @ApiOperation("修改密码")
    @RequestMapping(
            value = {"/data/changepwd"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JSONObject changepwd(String oldpwd,String newpwd) {
        SysUser sysUser = UserSession.getUser();

        if (sysUserService.changePwd(sysUser.getLoginId(), oldpwd,newpwd)>0) {
            return JSONResult.newBuilder().success("密码修改成功").build();
        } else {
            return JSONResult.newBuilder().fail("原密码错误").build();
        }
    }

    @ApiOperation("注销")
    @RequestMapping(
            value = {"/page/logout"},
            method = {RequestMethod.GET}
    )
    public String logoutView() {
        UserSession.removeUser();
        return "redirect:"+loginPageUrl;
    }

    @ApiOperation("注销")
    @RequestMapping(
            value = {"/data/logout"},
            method = {RequestMethod.POST}

    )
    @ResponseBody
    public JSONObject dataLogout() {
        UserSession.removeUser();
        return JSONResult.newBuilder().success("退出成功").build();
    }

}
